﻿Imports System.Windows.Forms

Public Class frmImportSB19

    Private DatCnvProcess As Process = Nothing

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        SaveForm()
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub btnPSA_Click(sender As Object, e As EventArgs) Handles btnPSA.Click
        Try
            With New OpenFileDialog
                .AddExtension = True
                .CheckFileExists = True
                .CheckPathExists = True
                .DefaultExt = ".psa"
                .FileName = txtConfig.Text
                .Filter = "SB-19 Program Setup Files (*.psa)|*.psa"
                .InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\WRDB"
                If .ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
                    txtPSA.Text = .FileName
                End If
            End With
        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

    Private Sub btnConfig_Click(sender As System.Object, e As System.EventArgs) Handles btnConfig.Click
        Try
            With New OpenFileDialog
                .AddExtension = True
                .CheckFileExists = True
                .CheckPathExists = True
                .DefaultExt = ".xmlcon"
                .FileName = txtConfig.Text
                .Filter = "SB-19 Configuration Files (*.xmlcon)|*.xmlcon"
                .InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\WRDB"
                If .ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
                    txtConfig.Text = .FileName
                End If
            End With
        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

    Private Sub btnProgram_Click(sender As System.Object, e As System.EventArgs) Handles btnProgram.Click
        Try
            With New FolderBrowserDialog
                .Description = "Select folder where Seabird software installed"
                .SelectedPath = txtProgram.Text
                If .ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
                    If Not My.Computer.FileSystem.FileExists(.SelectedPath & "\DatCnvW.exe") Then
                        WarningMsg("The required Seabird software was not found in the indicated directory; make sure you install it before continuing.")
                    End If
                    txtProgram.Text = .SelectedPath
                End If
            End With
        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

    Private Sub LoadForm()
        Try
            GetControlValue(REGAPP, txtProgram)
            GetControlValue(REGAPP, txtPSA)
            GetControlValue(REGAPP, txtConfig)
            Project.FillPickList(cboStationID, "", "Stations", "Station_ID", "Station_Name", "(Create New)", GetSetting(REGAPP, Me.Name, cboStationID.Name))
            GetControlValue(REGAPP, txtDateTime)
            Project.FillPickList(cboCCode, "", "CCodes", "CCode", "CCode_Name", "", GetSetting(REGAPP, Me.Name, cboCCode.Name))
            For i As Integer = 0 To 5
                Dim cbo As ComboBox = Choose(i + 1, cboTemp, cboCond, cboDO, cboPH, cboTurb, cboSalin)
                Project.FillPickList(cbo, "", "PCodes", "PCode", "Anal_Name", "(Ignore)", GetSetting(REGAPP, Me.Name, cbo.Name))
            Next
        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

    Private Sub SaveForm()
        Try
            SaveControlValue(REGAPP, txtProgram)
            SaveControlValue(REGAPP, txtPSA)
            SaveControlValue(REGAPP, txtConfig)
            SaveSetting(REGAPP, Me.Name, cboStationID.Name, Project.GetPickList(cboStationID))
            SaveControlValue(REGAPP, txtDateTime)
            SaveSetting(REGAPP, Me.Name, cboCCode.Name, Project.GetPickList(cboCCode))
            For i As Integer = 0 To 5
                Dim cbo As ComboBox = Choose(i + 1, cboTemp, cboCond, cboDO, cboPH, cboTurb, cboSalin)
                SaveSetting(REGAPP, Me.Name, cbo.Name, Project.GetPickList(cbo))
            Next
        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

    Private Sub frmImportSB19_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Try
            If DatCnvProcess IsNot Nothing Then DatCnvProcess.Kill()
        Catch ex As Exception
        End Try
    End Sub

    Private Sub frmImportSB19_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        LoadForm()
    End Sub

    Private Sub txtDateTime_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles txtDateTime.Validating
        Try
            Dim dt As Date = Nothing
            If Not DateTime.TryParse(txtDateTime.Text, dt) Then
                WarningMsg("The date-time you entered is invalid; please use format appropriate to your current culture settings.")
                e.Cancel = True
            End If
        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

    Private Sub btnConfigure_Click(sender As System.Object, e As System.EventArgs) Handles btnConfigure.Click
        Dim FileName As String = txtProgram.Text & "\datcnvw.exe"
        If Not My.Computer.FileSystem.FileExists(FileName) Then
            WarningMsg("The Seabird data conversion program was not found on the indicated path: " & FileName)
        Else
            DatCnvProcess = Process.Start(FileName)
        End If
    End Sub

    Private Sub cboStationID_SelectionChangeCommitted(sender As Object, e As System.EventArgs) Handles cboStationID.SelectionChangeCommitted
        With cboStationID
            If .Text.StartsWith("(") Then 'prompt for new station ID
                Dim StationID As String = InputBox("Enter the new Station ID you want to add; after importing, you will need to edit the Stations Support table to add information such as station name, latitude, longitude, etc.:", "New Station ID").ToUpper
                If StationID = "" Then
                    .SelectedIndex = 0
                Else
                    If StationID.Length > 25 Then
                        WarningMsg("The Station ID must be no more than 25 characters.")
                    ElseIf Project.DB.GetRecordList("Stations", "Station_ID").Contains(StationID) Then
                        WarningMsg("The Station ID you entered already exists.")
                    Else
                        Project.DB.ExecuteNonQuery($"INSERT INTO {Project.FullTableName(clsProject.enumTableTypes.Stations)} (Station_ID) VALUES ('{StationID}')")
                        Project.DB.UpdateRecordTimeStamp("Stations", String.Format("WHERE Station_ID='{0}'", StationID))
                        Project.FillPickList(cboStationID, "", "Stations", "Station_ID", "Station_Name", "(Create New)", StationID)
                    End If
                End If
            End If
        End With
    End Sub

    Private Sub btnJournal_Click(sender As System.Object, e As System.EventArgs) Handles btnJournal.Click
        frmJournal.GetJournalDescription()
    End Sub

End Class
